package com.onpositive.mediawiki.serializing;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;

import com.onpositive.data.IPropertyConstants;
import com.onpositive.data.IPropertyMetadata;
import com.onpositive.data.serializer.writers.AbstractWriter;
import com.onpositive.knowledge.model.IThing;
import com.onpositive.semantic.model.api.property.IProperty;
import com.onpositive.semantic.model.api.property.PropertyAccess;
import com.onpositive.units.ParsedFileList;

abstract class AbstractPicturePropertyMetadata implements IPropertyMetadata, IDefineWriterMetadata {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected LinkedHashMap<String,Object> map ;
	private IProperty picturePathProperty ;
	private IProperty pictureListProperty ; 
	
	public AbstractPicturePropertyMetadata( IProperty prop, String imageType, float quality, int width, int height,IProperty pl )
	{
		this.picturePathProperty = prop ;
		this.pictureListProperty=pl;
		map = new LinkedHashMap<String, Object>() ;
		map.put( PictureWriter.KEY_IMAGE_TYPE, imageType ) ;
		map.put( PictureWriter.KEY_COMPRESSION_QUALITY, quality ) ;
		map.put( PictureWriter.KEY_IMAGE_WIDTH, width ) ;
		map.put( PictureWriter.KEY_IMAGE_HEIGHT, height ) ;
		//map.put( IPropertyConstants.CHAR_PARSER_ID_LABEL,ParsedString.ID );		
	}
	
	public AbstractWriter<?> getWriter()
	{
		return new PictureWriter( (String) map.get( PictureWriter.KEY_IMAGE_TYPE ),
								  (Float)  map.get( PictureWriter.KEY_COMPRESSION_QUALITY),
								  (Integer)map.get( PictureWriter.KEY_IMAGE_WIDTH ),
								  (Integer)map.get( PictureWriter.KEY_IMAGE_HEIGHT )
								 );
	}
	

	@Override
	public Collection<String> keys()
	{
		return map.keySet();
	}

	@Override
	public Class<?> type() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object getMetaPropertyValue(String key)
	{
		return map.get( key );
	}

	//returns HDD-path of the picture 
	@Override
	public Object getValue(Object obj) {
		Object value = picturePathProperty.getValue(obj);
		if (value==null){
			IProperty property = pictureListProperty;
			if (property!=null){
				ParsedFileList value2 = (ParsedFileList) property.getValue(obj);
				if (value2!=null){
				ArrayList<String> valuesList = value2.getValuesList();
				for (String s:valuesList){
					value=s;
					break;
				}
				}
			}
		}
		if ((value!=null)&&(value.toString().indexOf('/')==-1||value.toString().startsWith("http://"))){
			IThing m=(IThing) obj;
			Object longDefaultImage = m.getLongDefaultImage();
			//System.out.println(longDefaultImage);
			if (longDefaultImage!=null){
				return longDefaultImage.toString();
			}
		}
		return value;
	}

}
